package com.hean.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.hean.util.Action;
import com.hean.util.DB;

public class SQLite extends Activity{
	private DB db;
	private ListView lv;
	private Cursor cursor;

	private String[] menuOptions = { "新建联系人", "导出联系人", "导入联系人" };

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		for (int i = 0; i < menuOptions.length; i++) {
			menu.add(0, i, i, menuOptions[i]);
		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == 0) {
			// Create
		} else if (id == 1) {
			// Export
		} else if (id == 2) {
			// Import
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sqlite);
		lv = (ListView) findViewById(R.id.sqlite_lv);

		db = new DB(SQLite.this);
		cursor = db.findAllContacts();

		lv.setAdapter(new SimpleCursorAdapter(SQLite.this,
				R.layout.sqlite_lv, cursor,
				new String[] { "name", "phone" }, new int[] {
						R.id.sqlite_name, R.id.sqlite_phone }));

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) {
				Builder builder = new AlertDialog.Builder(SQLite.this);
				builder.setTitle("请选择操作");
				builder.setItems(new String[] { "更新联系人", "删除联系人" },
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if (which == 0) {
									// Update
								} else if (which == 1) {
									// Delete
									new DeleteAction(position).execute();
								}
							}
						});
				builder.create().show();
			}
		});
	}

	private class DeleteAction implements Action {
		private int position;

		DeleteAction(int position) {
			this.position = position;
		}

		@Override
		public void execute() {
			Builder builder = new AlertDialog.Builder(SQLite.this);
			builder.setTitle("确定删除");
			builder.setMessage("真的删吗?");
			builder.setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (cursor.moveToPosition(position)) {
						int id = cursor.getInt(0);
						db.deleteContact(id);
						cursor.requery();//cursor中的数据一更新，SimpleCursorAdapter会更新ListView
					}
				}
			});
			builder.setNegativeButton("取消", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			builder.create().show();
		}

	}
	
	
	
	
	
//	private DB db;
//
//	@Override
//	protected void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//
//		db = new DB(SQLite.this);
//		db.findAllContacts();
//
//		// SQLiteDatabase db = openOrCreateDatabase("outman",
//		// SQLiteDatabase.OPEN_READONLY, null);
//		// db.execSQL("insert ....");
//		// Cursor cursor = db.rawQuery("select * from emp", null);
//		//
	
//		// if (cursor.moveToFirst()) {//是否需要moveToFirst，直接while(moveToNext)
//		// do {
//		// cursor.getString(1);
//		// } while (cursor.moveToNext());
//		// }
//
//		// cursor.moveToFirst();
//		// cursor.moveToLast();
//		// cursor.moveToNext();
//		// cursor.moveToPrevious()
//		// cursor.move(int offset);
//
//		db.close();
//	}
}
